$(document).ready(function(){        //DOM的onload事件处理函数    
	
	$("input,textarea").focus(function(){
	$("#msg").text("");
	var errorId = $(this).attr("id");
	$("#"+errorId).parent().find('.error').text('');
	});
	
 	$("#new_topic").submit(function(){
 		var bool = true;
 		$("#msg").text("");
 		if(!postdata()){
 			bool = false;
 		}
 		return bool;
 	})
	
 	$("#new_reply").submit(function(){
 		var bool = true;
 		$("#msg").text("");
 		if($("#reply_content").val().length <3){
 			var html = "<div class='alert alert-dismissable alert-danger fade in' " +
			"aria-hidden='true' id='error'> </div>";
			$("#reply_content").focus();
			$('#msg').append(html);
			$("#error").text('回复应内容不小于两个字符.')
			bool = false;
		}
		return bool;
 	})
});

//对回复内容check
function check_rcontent(){
	var bool = true;
	var html = "<div class='alert alert-dismissable alert-danger fade in' " +
			"aria-hidden='true' id='error'> </div>";
	if($("#reply_content").val().length <3){
		$("#reply_content").focus();
		$('#msg').append(html);
		$("#error").text('回复应内容不小于两个字符.');
		bool = false;
	} 
	return bool;
}

function postdata(){
	var bool = true;
	var topic_title = $("#topic_title").val();
	var content = $("#topic_content").val();
	var html = "<div class='alert alert-dismissable alert-danger fade in' " +
			"aria-hidden='true' id='error'> </div>";
	if(topic_title.length<7){
		$('#msg').append(html);
		$("#error").text('标题应不少于六个字符.');
		bool = false;
	}else if(content.length<7){
		$('#msg').append(html);
		$("#error").text('内容不少于六个字符.');
		bool = false;
	}
	return bool;
}
/*
function postdata(){                             //提交数据函数
var comment=htmldecode($("#reply_content").val());
  $.ajax({                                                 //调用jquery的ajax方法   
    type: "POST",                                     //设置ajax方法提交数据的形式    
    url: baseurl+"index.php/comment/add_comment",     //把数据提交到ok.php 
    data: "comment="+comment+"&fid="+$("#fid").val()+"&is_top="+$("#is_top").val()+"&username="+$("#username").val()+"&avatar="+$("#avatar").val()+"&layer="+$("#layer").val(),    //输入框writer中的值作为提交的数据  
    dataType: 'json',  
    success: function(msg){                 //提交成功后的回调，msg变量是ok.php输出的内容。
    var html="<article><div class='cell hoverable reply' id='comment_988'><table border='0' cellpadding='0' cellspacing='0' width='100%'><tr><td valign='top' width='48'><a href='"+siteurl+"user/info/"+msg.uid+"' class='profile_link' title='"+msg.username+"'><img alt='"+msg.username+" medium avatar' class='medium_avatar' src='"+msg.avatar+"' /></a></td><td width='10'></td><td valign='top' width='auto'><div class='pull-right'><small class='snow'>#<span id='add'>"+msg.layer+"</span> -"+msg.replytime+"<a href='#reply' onclick='replyOne('"+msg.username+"');'><img align='absmiddle' alt='Reply_button' border='0' id='mention_button' class='clickable mention_button' data-mention='"+msg.username+"' src='"+baseurl+"static/common/images/reply_button.png' /></a></small></div><a href='"+siteurl+"user/info/"+msg.uid+"' class='dark startbbs profile_link' title='"+msg.username+"'>"+msg.username+"</a><span class='snow'>&nbsp;&nbsp;</span><div class='sep5'></div><div class='content reply_content'>"+msg.content+"</div></div></td></tr></table></div></article>";
    $('#clist').append(html);
    $('#comments').html(msg.layer);//改变回复数
      //alert("数据提交成功");                     //如果有必要，可以把msg变量的值显示到某个DIV元素中    
    }
  });
}*/

//快速回复ctrl+enter
    $(document).keypress(function(e){
        var active_id = document.activeElement.id;  
        if((e.ctrlKey && e.which == 13 || e.which == 10) && (active_id == "topic_content" || active_id == "reply_content")) {
            e.preventDefault();
          //  $("#new_topic").submit();
          $("#new_topic").submit();
        }
    });

function replyOne(username){
    replyContent = $("#reply_content");
	oldContent = replyContent.val();
	prefix = "@" + username + " ";
	newContent = ''
	if(oldContent.length > 0){
	    if (oldContent != prefix) {
	        newContent = oldContent + "\n" + prefix;
	    }
	} else {
	    newContent = prefix
	}
	replyContent.focus();
	replyContent.val(newContent);
	moveEnd(replyContent);
}

